/* ****************************************************************************
 * Author: 671643387
 * Date: 2015/3/17
 * ***************************************************************************/

Ext.define("sggm.utils.Http", {
    statics: {
        url: 'http://172.31.244.3:7701/',
        method: 'GET',
        act: '',
        authKey: '',
        authGM: '',
        openid: '',
        uid: '',
        key: '',
        app: null,
        request: function(config, cbSuccess, cbFailure){
            config = config || {};
            var me = this;
            var method = config["method"] || this.method;
            var url = config["url"] || this.url;
            var act = config["act"] || this.act;
            var params = {
                openid: this.openid,
                uid: this.uid,
                key: this.key,
                act: act,
                authGM: this.authGM || sggm.utils.Cookie.get("authGM"),
                loginKey: this.authKey || sggm.utils.Cookie.get("authKeyXB")
            };
            if (typeof config["params"] == "object") {
                Ext.apply(params, config["params"]);
            }

            console.log("[HTTP REQ]: " + JSON.stringify(params));

            var loadMask = new Ext.LoadMask(Ext.getBody(), {
                msg: "请稍等...",
                removeMask: true
            });
            loadMask.show();

            Ext.Ajax.request({
                method: method,
                url: url,
                success: function(response){
                    loadMask.hide();
                    console.log("[RESP]: " + response.responseText);
                    var res = null;
                    try { res = JSON.parse(response.responseText) } catch(e) {
                        res = {
                            code: 1,
                            desc: response.responseText,
                            data: {}
                        };
                    }
                    if (res["code"] == -2) { // 没有权限
                        Ext.MessageBox.alert("警告", "你没有该操作的访问权限！");
                    } else if (res["code" == -1]) { // 重新登录
                        Ext.MessageBox.alert("警告", "登录过期，请重新登录！");
                        //me.reLogin();
                    }
                    cbSuccess(res);
                },
                failure: function(){
                    loadMask.hide();
                    cbFailure();
                },
                params: params
            });
        },

        requestFile: function(config, cbSuccess, cbFailure) {
            config = config || {};
            var method = config["method"] || this.method;
            var url = config["url"] || this.url;
            var filePath = config["file"] || "";
            var params = {
                authGM: this.authGM || sggm.utils.Cookie.get("authGM"),
                loginKey: this.authKey || sggm.utils.Cookie.get("authKeyXB")
            };
            if (typeof config["params"] == "object") {
                Ext.apply(params, config["params"]);
            }

            Ext.Ajax.request({
                method: method,
                url: url + filePath,
                success: function(response){
                    cbSuccess(response.responseText);
                },
                failure: function(){
                    cbFailure();
                },
                params: params
            });
        }
    }
});
